package com.cloud.pole.shop.controller.goods.admin;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.rest.BaseController;
import com.cloud.pole.common.util.jwt.IJWTInfo;
import com.cloud.pole.common.util.jwt.JwtTokenUtil;
import com.cloud.pole.shop.biz.goods.ShopGoodsBiz;
import com.cloud.pole.shop.biz.goods.ShopGoodsGroupGoodsBiz;
import com.cloud.pole.shop.model.entity.goods.ShopGoods;
import com.cloud.pole.shop.model.entity.goods.ShopGoodsAdmin;
import com.cloud.pole.shop.model.entity.goods.ShopGoodsAttr;
import com.cloud.pole.shop.model.entity.goods.ShopGoodsAttrValue;
import com.cloud.pole.shop.model.entity.goods.ShopGoodsGroupGoods;
import com.cloud.pole.shop.model.entity.goods.ShopGoodsModelAttr;
import com.cloud.pole.shop.model.entity.goods.ShopGoodsModelAttrValue;
import com.cloud.pole.shop.model.vo.goods.GoodsDetailVo;
import com.cloud.pole.shop.service.goods.GoodsService;
import com.cloud.pole.shop.service.goods.GoodsAttrService;
import com.cloud.pole.shop.service.goods.GoodsAttrValueService;
import com.cloud.pole.shop.service.goods.GoodsModelAttrService;
import com.cloud.pole.shop.service.goods.GoodsModelAttrValueService;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/goods/goods/admin")
@Slf4j
public class GoodsAdminController extends BaseController<ShopGoodsBiz, ShopGoods> {

	@Autowired
	private GoodsService goodsService;

	@Autowired
	private ShopGoodsGroupGoodsBiz shopGoodsGroupGoodsBiz;

	@Autowired
	private GoodsModelAttrService shopGoodsModelAttrService;

	@Autowired
	private GoodsModelAttrValueService goodsModelAttrValueService;
	
	@Autowired
	private GoodsAttrService goodsAttrService;

	@Autowired
	private GoodsAttrValueService goodsAttrValueService;
	/**
	 * 分页商品
	 * 
	 * @param pageNo
	 * @param pageSize
	 * @param mobile
	 * @param templateName
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/list")
	@ResponseBody
	public ApiRestResponse<ShopGoodsAdmin> list(Integer pageNo, Integer pageSize, String goodsName, String shopName,
			String mkCode, String mkCodeS, Integer mkGroups, Integer mkStatus,Integer shopType) {
		pageNo = (pageNo == null || pageNo < 1) ? 1 : pageNo;
		pageSize = (pageSize == null || pageSize < 1) ? 10 : pageSize;
		try {
			Map<String, Object> params = new HashMap<>();
			params.put("goodsName", goodsName);
			params.put("shopName", shopName);
			params.put("mkCode", mkCode);
			params.put("mkCodeS", mkCodeS);
			params.put("mkGroups", mkGroups);
			params.put("mkStatus", mkStatus);
			params.put("shopType", shopType);  
			PageInfo<ShopGoodsAdmin> pageInfo = goodsService.selectPageShopGoodsAdminList(params, pageNo, pageSize);
			return ApiRestResponse.successOfPage("成功", (int) pageInfo.getTotal(), pageInfo.getPages(),
					pageInfo.getPageSize(), pageInfo.getList());
		} catch (Exception e) {
			e.printStackTrace();
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(e.toString(), null);
		}
	}

	@RequestMapping(value = "/getGoodsInfo/{id}", method = RequestMethod.GET)
	@ResponseBody
	public ApiRestResponse<Map<String, Object>> getOrderInfo(@PathVariable String id) {
		try {
			GoodsDetailVo goodsDetail = null;
			List<ShopGoodsAttr> shopGoodsAttr = null;
			List<ShopGoodsAttrValue> shopGoodsAttrValue=null;
			String msg = "成功";
			if (id != null) {
				goodsDetail = goodsService.selectShopGoodsAdminById(id);
				List<ShopGoodsGroupGoods> selectByGoodsId = shopGoodsGroupGoodsBiz.selectByGoodsId(Integer.valueOf(id));
				goodsDetail.setShopGoodsGroupGoodsList(selectByGoodsId);
				shopGoodsAttr = goodsAttrService.selectByGoodsId(Integer.valueOf(id));
				shopGoodsAttrValue=goodsAttrValueService.selectByGoodsId(Integer.valueOf(id));
			} else {
				msg = "商品编号不能为空";
			}
			Map<String, Object> map = new HashMap<String,Object>();
			map.put("goodsDetail", goodsDetail);
			map.put("shopGoodsAttr", shopGoodsAttr);
			map.put("shopGoodsAttrValue", shopGoodsAttrValue);
			return ApiRestResponse.success(msg, map);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(e.toString(), null);
		}
	}

	/**
	 * 新增商品
	 * <p>
	 * Title: addGoods
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/addGoods", method = RequestMethod.POST)
	@ResponseBody
	public ApiRestResponse<ShopGoods> addGoods(@RequestBody Map map) {
		try {
			String token = request.getHeader(CommonConstants.AUTH_USER_TOKEN_HEADER);
			goodsService.saveGoods(token, map);
			return ApiRestResponse.success("成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(e.toString(), null);
		}
	}

	@RequestMapping(value = "/updateGoods/{id}", method = RequestMethod.PUT)
	@ResponseBody
	public ApiRestResponse<ShopGoods> updateGoods(@RequestBody Map map) {
		try {
			String token = request.getHeader(CommonConstants.AUTH_USER_TOKEN_HEADER);
			goodsService.updateGoods(token, map);
			return ApiRestResponse.success("成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(e.toString(), null);
		}
	}

	/**
	 * 删除商品
	 * <p>
	 * Title: deleteGoods
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/deleteGoods/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public ApiRestResponse<ShopGoods> deleteGoods(@PathVariable int id) {
		try {
			String token = request.getHeader(CommonConstants.AUTH_USER_TOKEN_HEADER);
			goodsService.deleteGoods(token, id);
			return ApiRestResponse.success("成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(e.toString(), null);
		}
	}

	@GetMapping("/getSelectByShopsB/{id}")
	@ResponseBody
	public ApiRestResponse<List<ShopGoods>> selectGoods(@PathVariable String id) {
		try {
			List<ShopGoods> selectShopGoodsByShopId = goodsService.selectShopGoodsByShopId(Integer.valueOf(id));
			return ApiRestResponse.success("成功", selectShopGoodsByShopId);
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return ApiRestResponse.fail(e.toString(), null);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiRestResponse.fail(e.toString(), null);
		}

	}

	@GetMapping("/getByModelList/{id}")
	@ResponseBody
	public ApiRestResponse<List<ShopGoodsModelAttr>> selectByList(@PathVariable String id) {
		try {
			List<ShopGoodsModelAttr> selectByList = shopGoodsModelAttrService.selectByList(Integer.parseInt(id));
			return ApiRestResponse.success("200", selectByList);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiRestResponse.fail(e.toString(), null);
		}
	}

	@GetMapping("addGoodsModel")
	@ResponseBody
	public ApiRestResponse<ShopGoodsModelAttr> addGoodsModel(String modelAttrName,String shopId) {
		try {
			String token = request.getHeader(CommonConstants.AUTH_USER_TOKEN_HEADER);
			ShopGoodsModelAttr selectByName = shopGoodsModelAttrService.selectBy(modelAttrName,Integer.valueOf(shopId));
			if(selectByName==null) {
				ShopGoodsModelAttr shopGoodsModelAttr = new ShopGoodsModelAttr();
				shopGoodsModelAttr.setShopId(Integer.valueOf(shopId));
				shopGoodsModelAttr.setModelAttrName(modelAttrName);
				IJWTInfo jwtInfo = JwtTokenUtil.getInfoFromToken(token);
				shopGoodsModelAttr.setMerchantId(1);
				shopGoodsModelAttr.setCreateTime(new Date());
				shopGoodsModelAttr.setCreateUserId(jwtInfo.getId());
				shopGoodsModelAttrService.addGoodsModelAttr(shopGoodsModelAttr);
				return ApiRestResponse.success("200", null);
			}else {
				return ApiRestResponse.success("300", null);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return ApiRestResponse.fail(e.toString(), null);
		}

	}

	@GetMapping("selectByGoodsModelVal")
	@ResponseBody
	public ApiRestResponse<Map<String, Object>> selectByGoodsModelVal(String attrValueName,String shopId) {
		try {
			ShopGoodsModelAttr selectByName = shopGoodsModelAttrService.selectByName(attrValueName,Integer.valueOf(shopId));
			Integer modelAttrId = selectByName.getModelAttrId();
			Integer shopGoodsId = selectByName.getShopId();
			List<ShopGoodsModelAttrValue> selectByList = goodsModelAttrValueService.selectByList(modelAttrId, shopGoodsId);
			Map<String, Object> map  = new HashMap<String,Object>();
			map.put("selectByName", selectByName);
			map.put("selectByList", selectByList);
			return ApiRestResponse.success("200", map);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiRestResponse.fail(e.toString(), null);
		}
	}
	
   @GetMapping("addGoodsModelVal")
   @ResponseBody
   public ApiRestResponse<ShopGoodsModelAttrValue> addGoodsModelVal(@RequestParam("attrValueName[]") String attrValueName,String modelAttrId,String shopId){

		try {
			String token = request.getHeader(CommonConstants.AUTH_USER_TOKEN_HEADER);
	        JSONArray jsonArray = JSONArray.parseArray(attrValueName);
			for(int i =0;i<jsonArray.size();i++) {
				String name = (String) jsonArray.get(i);
				ShopGoodsModelAttrValue selectBy = goodsModelAttrValueService.selectBy(name,Integer.valueOf(modelAttrId),Integer.valueOf(shopId));
				if(selectBy==null) {
					ShopGoodsModelAttrValue goodsModelAttrValue = new ShopGoodsModelAttrValue();
					IJWTInfo jwtInfo = JwtTokenUtil.getInfoFromToken(token);
					goodsModelAttrValue.setCreateTime(new Date());
					goodsModelAttrValue.setCreateUserId(jwtInfo.getId());
					goodsModelAttrValue.setMerchantId(1);
					goodsModelAttrValue.setModelAttrId(Integer.valueOf(modelAttrId));
					goodsModelAttrValue.setShopId(Integer.valueOf(shopId));
					goodsModelAttrValue.setAttrValueName(name);
					goodsModelAttrValueService.addGoodsModelVal(goodsModelAttrValue);
				}
			}
			return ApiRestResponse.success("200", null);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiRestResponse.fail(e.toString(), null);
		}

   }
   
}
